package com.dps.clz.utils;

import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

@Slf4j
public class TimeUtil {
    private static final Logger logger = LoggerFactory.getLogger(TimeUtil.class);

    public static String getCurrentTimeHourAgo(int hours) {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.HOUR, cal.get(Calendar.HOUR) - hours);
        return df.format(cal.getTime());
    }

    public static String getCurrentTimeDaysAgo(int days) {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.DATE, cal.get(Calendar.DATE) - days);
        return df.format(cal.getTime());
    }

    public static String getCurrentTimeMinAgoS(int minute) {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S");
        Date date = new Date();
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.MINUTE, cal.get(Calendar.MINUTE) - minute);
        return df.format(cal.getTime());
    }

    public static String getStartDayAfter(String dateStr, int day) {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        try {
            date = df.parse(dateStr);
        } catch (ParseException e) {
            logger.error("parse time error,the input time is:{}", dateStr);
        }
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.DATE, cal.get(Calendar.DATE) + day);
        return df.format(cal.getTime());
    }

    public static String getStartDayHourAfter(String dateStr, int hour, DateFormat df) {
        DateFormat dfs = new SimpleDateFormat("yyyy-MM-dd-HH");
        Date date = null;
        try {
            date = dfs.parse(dateStr);
        } catch (ParseException e) {
            logger.error("parse time error,the input time is:{}", dateStr);
        }
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.HOUR, cal.get(Calendar.HOUR) + hour);
        return df.format(cal.getTime());
    }

    public static String getTimeDayAgo(int day) {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date();
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.DATE, cal.get(Calendar.DATE) - day);
        return df.format(cal.getTime());
    }

    /**
     * 如果 8点，格式是8非08
     *
     * @param hour
     * @return
     */
    public static String getTimeHoursAgo(int hour) {
        DateFormat df = new SimpleDateFormat("HH");
        Date date = new Date();
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.HOUR_OF_DAY, cal.get(Calendar.HOUR_OF_DAY) - hour);
        String timeHour = df.format(cal.getTime());
        return Integer.valueOf(timeHour).toString();
    }

    public static String getCurrentDay(int days) {
        DateFormat df = new SimpleDateFormat("yyyy_MM_dd");
        Date date = new Date();
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.DATE, cal.get(Calendar.DATE) - days);
        return df.format(cal.getTime());
    }

    public static String getStartDayHoursBefore(int hour) {
        DateFormat dfs = new SimpleDateFormat("yyyy-MM-dd-HH");
        Date date = new Date();
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.HOUR, cal.get(Calendar.HOUR) - hour);
        return dfs.format(cal.getTime());
    }


    /**
     * 相差天数+1
     *
     * @param dateStr2
     * @param dateStr1
     * @return
     */
    public static String getDifferentDays(String dateStr2, String dateStr1) {
        DateFormat format = new SimpleDateFormat("yyyyMMdd");
        try {
            Date date2 = format.parse(dateStr2);
            Date date1 = format.parse(dateStr1);
            int days = (int) ((date2.getTime() - date1.getTime()) / (1000 * 3600 * 24));
            return String.valueOf(days + 1);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return "1";
    }

    public static double getDiffHours(String dateStr1) {
        if (StringUtils.isEmpty(dateStr1)) return 0d;
        try {
            dateStr1 = dateStr1.substring(0, 19);
            DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date date1 = format.parse(dateStr1);
            Date date2 = new Date();
            double diffHours = (date2.getTime() - date1.getTime()) / (1000.0 * 3600);
            BigDecimal bd = new BigDecimal(diffHours);
            diffHours = bd.setScale(6, BigDecimal.ROUND_HALF_UP).doubleValue();
            return diffHours;
        } catch (Exception e) {
            log.error("getDiffHours error,the dateStr1:{}", dateStr1, e);
        }
        return 0d;
    }

    public static void main(String[] args) {
        System.out.println(getTimeHoursAgo(11));
    }
}
